// [[ OnflowGenerator File UUID: 0a6b1181144d48b991de601772a7ac82  ]]
package com.itdct.onflow.system.monitor.service

import com.itdct.onflow.core.constant.CommonString
import com.itdct.onflow.core.constant.ReturnCode
import com.itdct.onflow.core.extend.*
import com.itdct.onflow.core.vo.KeyLabelValueVo
import com.itdct.onflow.core.vo.RespPageVo
import com.itdct.onflow.core.vo.RespVo
import com.itdct.onflow.system.common.base.BaseSysService
import com.itdct.onflow.system.common.context.Context
import com.itdct.onflow.system.common.extend.dict
import com.itdct.onflow.system.monitor.entity.SysMachineInfoEntity
import com.itdct.onflow.system.monitor.mapper.SysMachineInfoMapper
import com.itdct.onflow.system.monitor.qo.SysMachineInfoEditQo
import com.itdct.onflow.system.monitor.qo.SysMachineInfoSearchQo
import com.itdct.onflow.system.monitor.vo.SysMachineInfoDetailVo
import com.itdct.onflow.system.monitor.vo.SysMachineInfoListVo
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

/**
 * @author DCTANT
 * @date 2025-10-13 10:48:15
 * @version 1.0
 * @description 服务器信息服务
 */
@Service
class SysMachineInfoService(
    val sysMachineInfoMapper: SysMachineInfoMapper
) : BaseSysService() {

    @Transactional(rollbackFor = [Exception::class])
    fun add(qo: SysMachineInfoEditQo, context: Context): RespVo<Long> {
        val addEntity = qo.toEntity(SysMachineInfoEntity::class.java)
        val id = sysMachineInfoMapper.insertEx(addEntity)
        return success(id)
    }

    @Transactional(rollbackFor = [Exception::class])
    fun update(qo: SysMachineInfoEditQo, context: Context): RespVo<Unit> {
        val updateEntity = sysMachineInfoMapper.selectByIdEx(qo.id)
        qo.injectValue(updateEntity)
        sysMachineInfoMapper.updateEx(updateEntity)
        return success()
    }

    fun delete(qo: SysMachineInfoEditQo, context: Context): RespVo<Unit> {
        val ids = qo.ids ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_DELETE_ID_LIST)
        val deleteEntities = sysMachineInfoMapper.selectByIdsEx(ids)
        sysMachineInfoMapper.deleteBatchEx(deleteEntities)
        return success()
    }

    fun detail(qo: SysMachineInfoSearchQo, context: Context): RespVo<SysMachineInfoDetailVo> {
        val id = qo.id ?: fail(ReturnCode.LACK_PARAM, CommonString.REQUIRE_ID)
        val vo = sysMachineInfoMapper.findById(id) ?: fail(CommonString.ENTITY_NOT_FOUND_USAGE("服务器信息", id))
        return success(vo.dict())
    }

    fun page(qo: SysMachineInfoSearchQo, context: Context): RespPageVo<SysMachineInfoListVo> {
        startPage(context)
        val pageData = sysMachineInfoMapper.findByList(qo)
        return successPage(pageData.dict())
    }

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]

    fun selectMachineIdList(qo: SysMachineInfoSearchQo, context: Context): RespVo<List<KeyLabelValueVo>> {
        val vos = sysMachineInfoMapper.selectMachineIdList(qo)
        return success(vos)
    }
// [[ OnflowGenerator Range (- selectFunctions -) End ]]
}